package com.wmd.core.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*类说明
*@author yincheng lu
*@time 2019年1月7日下午5:06:43
*@Description
*/
public class YKArrayUtils {
	public static boolean intersection(Class<?>[] a,Class<?>[] b) {
		for(int i=0;i<a.length;i++) {
			for(int j=0;j<b.length;j++) {
				if(a[i].equals(b[j])) {
					return true;
				}
			}
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> List<T> getClassFromArray(Object[] data,Class<T> object) {
		List<T> result=new ArrayList<>();
		for(int i=0;i<data.length;i++) {
			if(object.isInstance(data[i])) {
				result.add( (T) data[i]);
			}
		}
		return result;
	}
	public static List<Object> getOtherClassFromArray(Object[] data,Class<?> Excluseobject) {
		List<Object> result=new ArrayList<>();
		for(int i=0;i<data.length;i++) {
			if(!Excluseobject.isInstance(data[i])) {
				result.add(data[i]);
			}
		}
		return result;
	}
	public static List<Object> getOtherClassFromArray(Object[] data,Class<?>... Excluseobjects) {
		List<Object> result=new ArrayList<>();
		for(int i=0;i<data.length;i++) {
			boolean isExcluse=false;
			for(Class<?> c:Excluseobjects){
				if(c.isInstance(data[i])){
					isExcluse=true;
					break;
				}else{
					if(data[i] instanceof Collection){
						Collection collection=((Collection)data[i]);
						if(!collection.isEmpty()){
							if(c.isInstance(collection.iterator().next())){
								isExcluse=true;
								break;
							}
						}
					}
				}
			}
			if(!isExcluse) {
				result.add(data[i]);
			}
		}
		return result;
	}
	public static List<Object> getClassFromArrayInPackage(Object[] data,String packageString) {
		List<Object> result=new ArrayList<>();
		for(int i=0;i<data.length;i++) {
			if(data[i].getClass().getName().contains(packageString)) {
				result.add( data[i]);
			}
		}
		return result;
	}
	/*public static List<Annotation> getAnnotationFromArray(Field[] data,Class<? extends Annotation> annotation) {
		List<Annotation> result=new ArrayList<Annotation>();
		for(int i=0;i<data.length;i++) {
			Annotation an=data[i].getAnnotation(annotation);
			if(an!=null) {
				result.add(an);
			}
		}
		return result;
	} */
	@SuppressWarnings("unchecked")
	public static <T> List<T> getAnnotationFromArray(Field[] data,Class<? extends Annotation> annotation) {
		List<T> result=new ArrayList<T>();
		for(int i=0;i<data.length;i++) {
			Annotation an=data[i].getAnnotation(annotation);
			if(an!=null) {
				result.add((T) an);
			}
		}
		return result;
	} 
	public static List<Field> getFieldByAnnotation(Class<?> classData,Class<? extends Annotation> annotation){
		Field[] data=classData.getDeclaredFields();
		List<Field> result=new ArrayList<Field>();
		for(int i=0;i<data.length;i++) {
			Annotation an=data[i].getAnnotation(annotation);
			if(an!=null) {
				result.add(data[i]);
			}
		}
		return result;
	}
	
}
